package com.uzai.console.service.tb.tbstoreblacklist.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.tb.tbstoreblacklist.TbStoreBlacklistConfigDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.tb.tbstoreblacklist.TbStoreBlacklistConfigService;
import com.uzai.console.vo.tb.tbstoreblacklist.TbStoreBlacklistConfigVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 淘宝店铺黑名单基础设置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class TbStoreBlacklistConfigServiceImpl implements TbStoreBlacklistConfigService {

    Logger logger = LoggerFactory.getLogger(TbStoreBlacklistConfigServiceImpl.class);

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询淘宝店铺黑名单基础设置详情
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param merId
     * @return null
     */
    public TbStoreBlacklistConfigVo findTbStoreBlacklistConfig(Long merId){
        TbStoreBlacklistConfigVo tbStoreBlacklistConfigVo = new TbStoreBlacklistConfigVo();
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.TB_STORE_BLACKLIST_CONFIG.getValue());
        if(merBaseConfig != null){
            tbStoreBlacklistConfigVo = JSONObject.parseObject(merBaseConfig.getVal(), TbStoreBlacklistConfigVo.class);
        }
        return tbStoreBlacklistConfigVo;
    }

    /**
     * 修改淘宝店铺黑名单基础设置
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param tbStoreBlacklistConfigDto
     * @return null
     */
    public void updateTbStoreBlacklistConfig(Long merId, TbStoreBlacklistConfigDto tbStoreBlacklistConfigDto){

        if(tbStoreBlacklistConfigDto == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "淘宝店铺黑名单基础设置不能为空");
        }

        //查询当前的配置
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.TB_STORE_BLACKLIST_CONFIG.getValue());
        Integer now = DateUtil.getNowTime();
        //如果存在则修改
        if(merBaseConfig != null){//修改
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(tbStoreBlacklistConfigDto)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        }else{ //不存在就添加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(merId);
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.TB_STORE_BLACKLIST_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(tbStoreBlacklistConfigDto)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.TB_STORE_BLACKLIST_CONFIG.getKey(), merId), Tools.getStrEmpty(JSONObject.toJSONString(tbStoreBlacklistConfigDto)));

    }
}
